package com.zsq.thread.test7;

import java.util.concurrent.BrokenBarrierException;
//todo 线程堆满才开始执行
//todo 使用场景   复杂操作 需要等其他线程执行完,才能执行  (这个不是使用场景)限流常用google的Guava RateLimiter
public class CyclicBarrier {
    public static void main(String[] args) {
        //CyclicBarrier barrier = new CyclicBarrier(20);
        //线程堆满20个才开始执行
        java.util.concurrent.CyclicBarrier barrier = new java.util.concurrent.CyclicBarrier(20, () -> System.out.println("=======满人,发车======="));

        /*CyclicBarrier barrier = new CyclicBarrier(20, new Runnable() {
            @Override
            public void run() {
                System.out.println("满人，发车");
            }
        });*/

        for(int i=0; i<100; i++) {

            new Thread(()->{
                try {
                    barrier.await();
                    System.out.println("执行操作");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        }
    }
}
